package co.wizardattack.model;

import java.io.IOException;

import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.opengl.texture.TextureManager;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import co.wizardattack.constants.ResourcesNames;

import android.content.Context;

public class Wizard {

	private Context context;

	/**
	 * Constantes
	 */
	private final int IMAGE_WIDTH = 1080;
	private final int IMAGE_HEIGHT = 720;
	private final int IMAGE_COLUMNS = 3;
	private final int IMAGE_ROWS = 4;
	private final String IMAGE_NAME_FILE = "secuencia.png";

	/**
	 * Atributos
	 */
	private float posX = 300;
	private float posY = 275;
	private BitmapTextureAtlas atlasAnimatedWizard;
	private ITiledTextureRegion textureAnimatedWizard;
	private AnimatedSprite spriteAnimatedWizard;

	public Wizard(Context context) {
		this.context = context;		
	}
	
	

	public void createResources(TextureManager textureManager) throws IOException {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath(ResourcesNames.IMAGES_PATH);
		atlasAnimatedWizard = new BitmapTextureAtlas(textureManager, IMAGE_WIDTH, IMAGE_HEIGHT,TextureOptions.DEFAULT);
		textureAnimatedWizard = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(atlasAnimatedWizard, context, IMAGE_NAME_FILE, 0, 0,IMAGE_COLUMNS, IMAGE_ROWS);
		atlasAnimatedWizard.load();
	}
	
	
	public void createScene(VertexBufferObjectManager vertexBffObjManager){
		this.spriteAnimatedWizard = new AnimatedSprite(posX, posY, textureAnimatedWizard,  vertexBffObjManager);		
	}

	public BitmapTextureAtlas getAtlasAnimatedWizard() {
		return atlasAnimatedWizard;
	}

	public void animatedBack() {	
		long[] duracionFrame = { 200, 200, 200 };
		spriteAnimatedWizard.animate(duracionFrame, 0, 2, true);
	}
	public void animatedRight() {	
		long[] duracionFrame = { 200, 200, 200 };
		spriteAnimatedWizard.animate(duracionFrame, 3, 5, true);
	}
	public void animatedFront() {	
		long[] duracionFrame = { 200, 200, 200 };
		spriteAnimatedWizard.animate(duracionFrame, 6, 8, true);
	}
	public void animatedLeft() {	
		long[] duracionFrame = { 200, 200, 200 };
		spriteAnimatedWizard.animate(duracionFrame, 9, 11, true);
	}

	//Para acceder y modificar posiciones del Wizard
	public float getPosX() {
		return spriteAnimatedWizard.getX();
	}
	public void setPosX(float posX) {
		spriteAnimatedWizard.setX(posX);
		this.posX = posX;
	}
	public float getPosY() {
		return spriteAnimatedWizard.getY();
	}
	public void setPosY(float posY) {
		spriteAnimatedWizard.setY(posY);
		this.posY = posY;
	}
}
